// datetime

export default class Datetime {
  constructor (timestr) {
    this.timestr = timestr
    // 分解时间字符串
    let array = timestr.split('T')
    let tmpDate = array[0]
    let tmpTime = array[1]
    let dateArray = tmpDate.split('-')
    let timeArray = tmpTime.split(':')
    this.year = dateArray[0]
    this.month = dateArray[1]
    this.day = dateArray[2]
    this.hour = timeArray[0]
    this.minute = timeArray[1]
    this.second = timeArray[2].substring(0, 2)
  }

  getYear () {
    return this.year
  }

  getDay () {
    return this.day
  }

  getMonth () {
    return this.month
  }

  getHour () {
    return this.hour
  }

  getMinute () {
    return this.minute
  }

  getSecond () {
    return this.second
  }

  getDateStr () {
    let dateStr = this.year + '年' + this.month + '月' + this.day + '日'
    return dateStr
  }

  getDatetimeStr () {
    let datetimeStr = this.year + '-' + this.month + '-' + this.day + ' ' + this.hour + ':' + this.minute + ':' + this.second
    return datetimeStr
  }

  getPastTime () {
    let now = new Date();
    let timeStr = this.month + ' ' + this.day + ',' + this.year  + ' ' + this.hour + ':' + this.minute + ':' + this.second
    let time = new Date(timeStr)
    let delta = (now.getTime() - time.getTime())/1000
    if (delta < 60) {
      return '刚刚'
    } else if (delta >= 60 && delta < 3600) {
      let minute = Math.floor(delta / 60)
      return minute + '分钟前'
    } else if (delta >= 3600 && delta < 86400) {
      let hour = Math.floor(delta / 3600)
      return hour + '小时前'
    } else if (delta >= 86400 && delta < 2592000) {
      let day = Math.floor(delta / 86400)
      return day + '天前'
    } else if (delta >= 2592000 && delta < 31104000) {
      let month = Math.floor(delta / 2592000)
      return month + '月前'
    } else if (delta > 31104000) {
      let year = Math.floor(delta / 31104000)
      return year + '年前'
    }
  }
}